package com.thread.test13;

import java.util.LinkedList;

/**
 * @author bin
 * @date 2025 - 03 -14
 **/
public class Box {
    /**
     * 水果盒
     * */
    private LinkedList<Fruit> boxList = new LinkedList<>();

    /**
     * 最大容量
     * */
    private  static  final int MAX_SIZE = 5;

    /**
     * 男孩子放水果
     * */
    public synchronized void put(String name,Fruit fruit) throws InterruptedException {
        while(this.boxList.size() < MAX_SIZE){
            this.boxList.add(fruit);
            System.out.println("\033[34m" + name+"放入水果"+fruit + "当前篮子容量" + boxList.size() + "\033[0m");
            Thread.sleep(1000);
        }
            this.notifyAll();
            try {
                this.wait();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }


    }

    /**
     * 女孩子取水果
     * */
    public synchronized void take(String name) throws InterruptedException {
        while (!this.boxList.isEmpty()) {
            Fruit fruit = this.boxList.removeLast();
            System.out.println("\033[31m" + name + "取出水果" + fruit + "当前篮子容量" + boxList.size() + "\033[0m");
            Thread.sleep(1000);
        }
        this.notifyAll();
        try {
            this.wait();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
}
